/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import com.kairos.engine.RTBeacon;

import fever.display.cursor.Cursor;
import fever.exception.IllegalAccessException;
import fever.Fever;
import fever.utils.Stringifier;

/**
 * Abstract cursor implementation.
 *  
 * @author Romain Ecarnot
 */
class fever.display.cursor.AbstractCursor implements Cursor
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _mcTarget : MovieClip;
	private var _nXOffset : Number;
	private var _nYOffset : Number;
	private var _bHideMouse : Boolean;
	
		
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Indicates if system cursor must be hidden or not.(default {@code false}).
	 * 
	 * @return {@code true} if system cursor must be hidden.
	 */
	public function hideSystemCursor( ) : Boolean
	{
		return _bHideMouse;
	}
	
	/**
	 * Sets cursor's {@code x} location offset.
	 */
	public function setXOffset( x : Number ) : Void
	{
		_nXOffset = x || 0;
	}
	
	/**
	 * Returns cursor {@code x} offset.
	 */
	public function getXOffset( ) : Number
	{
		return _nXOffset;
	}
	
	/**
	 * Sets cursor's {@code y} location offset.
	 */
	public function setYOffset( y : Number ) : Void
	{
		_nYOffset = y || 0;
	}
	
	/**
	 * Returns cursor {@code y} offset.
	 */
	public function getYOffset( ) : Number
	{
		return _nYOffset;
	}
	
	/**
	 * Shows cursor.
	 */
	public function show( ) : Void
	{
		RTBeacon.getInstance().addFrameListener( this );
		
		onEnterFrame();
		
		if( hideSystemCursor() ) Mouse.hide();
		else Mouse.show();
		
		_mcTarget._visible = true;
	}
	
	/**
	 * Hides cursor.
	 */
	public function hide( ) : Void
	{
		RTBeacon.getInstance().removeFrameListener( this );
		
		_mcTarget._visible = false;
		
		if( hideSystemCursor() ) Mouse.show();
	}
	
	/**
	 * Triggers at each displayed frame by the player.
	 */
	public function onEnterFrame() : Void
	{
		_repaint();
	}
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		return Stringifier.parse( this );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */ 
	private function AbstractCursor( hideMouse : Boolean ) 
	{
		_mcTarget = Fever.stage.createCursorTarget();
		_nXOffset = 0;
		_nYOffset = 0;
		_bHideMouse = ( hideMouse == true ) ? true : false;
		
		_paint();
	}
	
	private function _paint() : Void
	{
		throw new IllegalAccessException( this  + '._paint() must be override' );
	}
	
	private function _repaint() : Void
	{
		_mcTarget._x = _mcTarget._parent._xmouse + _nXOffset;
		_mcTarget._y = _mcTarget._parent._ymouse + _nYOffset;
	}
}